package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreFileLogic;
import cn.topevery.garden.dal.logic.SpecialScoreLogic;
import cn.topevery.garden.entity.dto.SpecialScoreDTO;
import cn.topevery.garden.entity.po.GreFile;
import cn.topevery.garden.entity.po.SpecialScore;
import cn.topevery.garden.entity.query.SpecialScoreQuery;
import cn.topevery.garden.entity.vo.SpecialScoreVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 特殊分数
 */
@Api(tags = "特殊分数")
@RestController
@RequestMapping("/specialScore")
public class SpecialScoreController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    SpecialScoreLogic specialScoreLogic;

    @Autowired
    GreFileLogic greFileLogic;

    /**
     * 获取特殊分数分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取特殊分数页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<SpecialScoreVO>> listPage(@RequestBody SpecialScoreQuery query) {
        DataPager<SpecialScoreVO> page = specialScoreLogic.listPage(query);
        if (!CollectionUtils.isEmpty(page.records)) {
            for (SpecialScoreVO specialScoreVO : page.records) {
                List<GreFile> fileList = greFileLogic.getListByMainId(specialScoreVO.getId(), GreFile.TYPE_TASK_ON_SITE);
                specialScoreVO.setFileList(fileList);
            }
        }
        return JsonResult.ok(page);
    }

    @ApiOperation("新增/编辑 特殊分数信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody SpecialScoreDTO dto) {
        String userId = getUserId();
        specialScoreLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, SpecialScore.class));
        // 删去旧的文件关联
        List<GreFile> oldFileList = greFileLogic.getListByMainId(dto.getId(), GreFile.TYPE_TASK_ON_SITE);
        if (!CollectionUtils.isEmpty(oldFileList)) {
            Integer[] oldFileIdList = oldFileList.stream().map(GreFile::getId).toArray(Integer[]::new);
            greFileLogic.deleteBatchById(userId, oldFileIdList);
        }
        // 新增文件关联
        List<GreFile> newFileList = dto.getFileList();
        if (!CollectionUtils.isEmpty(newFileList)) {
            for (GreFile greFile : newFileList) {
                greFile.mainId = dto.getId();
                greFile.setDataType(GreFile.TYPE_TASK_ON_SITE);
            }
            greFileLogic.insert(userId, newFileList);
        }
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<SpecialScoreVO> getInfoById(@RequestParam Integer id) {
        SpecialScore entity = specialScoreLogic.getEntity(id);
        SpecialScoreVO vo = mapper.map(entity, SpecialScoreVO.class);
        List<GreFile> fileList = greFileLogic.getListByMainId(id, GreFile.TYPE_TASK_ON_SITE);
        vo.setFileList(fileList);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        String userId = getUserId();
        specialScoreLogic.deleteBatchById(getUser().id.toString(), ids);
        Arrays.stream(ids).forEach(id -> {
                    List<GreFile> oldFileList = greFileLogic.getListByMainId(id, GreFile.TYPE_TASK_ON_SITE);
                    if (CollectionUtils.isEmpty(oldFileList)) {
                        Integer[] oldFileIdList = oldFileList.stream().map(GreFile::getId).toArray(Integer[]::new);
                        greFileLogic.deleteBatchById(userId, oldFileIdList);
                    }
                }
        );

        return JsonResult.ok(true);
    }

    @ApiOperation("导出特殊分数信息")
    @GetMapping("/export")
    public void export(SpecialScoreQuery query, HttpServletResponse response) {
        List<SpecialScoreVO> list = specialScoreLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "特殊分数", "sheet1", SpecialScoreVO.class, "特殊分数", response);
    }

}
